package com.jkoss.wine.investment.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.util.Constant;
import com.jkoss.common.util.CryptoUtils;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.investment.entity.Investment;
import com.jkoss.wine.investment.entity.InvestmentFollow;
import com.jkoss.wine.investment.service.IInvestmentFollowService;
import com.jkoss.wine.investment.utils.MyUtils;
import com.jkoss.wine.system.entity.MerchantEmployees;
import com.jkoss.wine.system.service.IMerchantEmployeesService;

/**
 * 招商跟进记录 前端控制器
 * 
 * @Author ym
 * @Version 1.0, 2019-08-13
 * @See
 * @Since com.jkoss.wine.investment.invfollow.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/investment/invfollow")
public class InvestmentFollowController extends BaseController {

	@Autowired
	private IInvestmentFollowService iInvestmentFollowService;
	@Autowired
	private IMerchantEmployeesService iMerchantEmployeesService;

	@RequestMapping("/list")
	@RequiresPermissions("/investment/invfollow/list")
	public String list(String id,DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request,
			HttpServletResponse response) {
		Wrapper wrapper = Condition.create().eq("i.del", "0").eq("imf.del", "0").eq("me.del", "0").eq("i.id", id);
		Page resultPage = iInvestmentFollowService.selectVoByPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		map.put("imid",id);
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			//默认排序字段
			wrapper.orderBy("cr.add_time", false);
		}
		System.out.println("id--------"+id);
		return "investment/invfollow/list";
	}

	@RequestMapping("/toInsert")
	@RequiresPermissions("/investment/invfollow/toInsert")
	public String toInsert(String id,ModelMap map,HttpServletRequest request, HttpServletResponse response) {
		System.out.println("toinsert:id---"+id);
		// 获取跟进人下拉列表
		Wrapper empWaper = Condition.create().eq("del", 0);
		List<MerchantEmployees> melist = iMerchantEmployeesService.selectList(empWaper);
		map.put("maids", melist);
		map.put("imid", id);
		return "investment/invfollow/edit";
	}

	@RequestMapping("/insert")
	@RequiresPermissions("/investment/invfollow/toInsert")
	@ResponseBody
	public Object insert(@Valid InvestmentFollow investment_follow, HttpServletRequest request,
			HttpServletResponse response) {
		// 添加时间
		investment_follow.setAddTime(CommonUtil.getDate());
		// 操作人
		investment_follow.setOperator(MyUtils.setOperator());
		// 是否删除 0否1是
		investment_follow.setDel(0);
		System.out.println("investment_follow:--------" + investment_follow);

		if (iInvestmentFollowService.insert(investment_follow)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}

	}

	@RequestMapping("/toUpdate")
	@RequiresPermissions("/investment/invfollow/toUpdate")
	public String toUpdate(String id, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		// 获取跟进人下拉列表
		Wrapper empWaper = Condition.create().eq("del", 0);
		List<MerchantEmployees> melist = iMerchantEmployeesService.selectList(empWaper);
		map.put("maids", melist);
		map.put("record", iInvestmentFollowService.selectById(id));
		System.out.println("进入toUp："+iInvestmentFollowService.selectById(id));
		return "investment/invfollow/edit";
	}

	@RequestMapping("/update")
	@RequiresPermissions("/investment/invfollow/toUpdate")
	@ResponseBody
	public Object update(@Valid InvestmentFollow investment_follow, HttpServletRequest request,
			HttpServletResponse response) {
		investment_follow.setOperator(MyUtils.setOperator());
		investment_follow.setFwtm(CommonUtil.getDate());
		investment_follow.setAddTime(CommonUtil.getDate());
		if (iInvestmentFollowService.updateById(investment_follow)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	@RequestMapping("/delete")
	@RequiresPermissions("/investment/invfollow/delete")
	@ResponseBody
	public Object delete(String[] id, HttpServletRequest request, HttpServletResponse response) {
		List list = new ArrayList();
		for (String string : id) {
			InvestmentFollow investmentFollow = new InvestmentFollow();
			investmentFollow.setId(string);
			// 是否删除：0-未删除、1-已删除、2-不能删除
			investmentFollow.setDel(1);
			list.add(investmentFollow);
		}
		if (iInvestmentFollowService.updateBatchById(list)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

}
